package cn.xuchunh.campusbbs.ui.fragments;

import android.net.Uri;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import cn.xuchunh.campusbbs.AppContext;
import cn.xuchunh.campusbbs.R;
import cn.xuchunh.campusbbs.adapter.SimpleIndicatorAdapter;
import cn.xuchunh.campusbbs.base.BaseRecyclerViewAdapter;
import cn.xuchunh.campusbbs.base.BaseRefreshListFragment;
import cn.xuchunh.campusbbs.bean.Indicator;
import cn.xuchunh.campusbbs.ui.activities.LoginActivity;
import cn.xuchunh.campusbbs.ui.fragments.dialog.ClearCacheDialog;
import cn.xuchunh.campusbbs.ui.fragments.dialog.CreditsDialog;
import cn.xuchunh.campusbbs.ui.fragments.dialog.ServerSettingDialog;
import cn.xuchunh.campusbbs.ui.fragments.dialog.UIModeSettingDialog;
import cn.xuchunh.campusbbs.utils.AppUtil;
import cn.xuchunh.campusbbs.viewholder.SimpleIconViewHolder;

/**
 * Created by XuChunH on 2015/10/9.
 */
public class SettingFragment extends BaseRefreshListFragment<SimpleIconViewHolder, Indicator>
        implements ClearCacheDialog.OnClearCacheListener {

    @Override
    protected RecyclerView.LayoutManager createLayoutManager() {
        return new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
    }

    @Override
    protected BaseRecyclerViewAdapter<SimpleIconViewHolder, Indicator> createAdapter() {
        return new SimpleIndicatorAdapter(getActivity());
    }

    @Override
    protected List<Indicator> parse(JSONObject jsonObject) throws JSONException {
        return null;
    }

    @Override
    protected void loadDataFromServer() {

    }

    @Override
    protected void loadDataFromCache() {
        List<Indicator> list = new ArrayList<>();
        list.add(new Indicator("服务器", Uri.parse("res://" + getActivity().getPackageName()
                + "/" + R.drawable.mc_forum_squre_icon7)));
        list.add(new Indicator("主页面样式", Uri.parse("res://" + getActivity().getPackageName()
                + "/" + R.drawable.mc_forum_squre_icon7)));
        list.add(new Indicator("版权信息", Uri.parse("res://" + getActivity().getPackageName()
                + "/" + R.drawable.mc_forum_squre_icon7)));
        list.add(new Indicator("清空缓存（" + AppUtil.getCacheSize(getActivity()) + "）",
                Uri.parse("res://" + getActivity().getPackageName() + "/" + R.drawable.mc_forum_squre_icon7)));
        list.add(new Indicator("创建快捷方式",Uri.parse("res://" + getActivity().getPackageName()
                + "/" + R.drawable.mc_forum_squre_icon7)));
        loadDataFinished(true, list);
    }

    @Override
    public String getVolleyTag() {
        return null;
    }

    @Override
    public String getCacheKey() {
        return null;
    }

    @Override
    protected void afterInitView() {
        loadData(false);
        getRefreshLayout().setEnabled(false);
    }

    @Override
    public void onItemClick(int position, View item) {
        if (position == 0) {
            new ServerSettingDialog().show(getFragmentManager(), ServerSettingDialog.class.getSimpleName());
        } else if (position == 1) {
            new UIModeSettingDialog().show(getFragmentManager(), UIModeSettingDialog.class.getSimpleName());
        } else if (position == 2) {
            new CreditsDialog().show(getFragmentManager(), CreditsDialog.class.getSimpleName());
        } else if (position == 3) {
            ClearCacheDialog clearCacheDialog = new ClearCacheDialog();
            clearCacheDialog.show(getFragmentManager(), ClearCacheDialog.class.getSimpleName());
            clearCacheDialog.setOnClearCacheListener(this);
        }else if(position == 4){
            AppUtil.createShortCut(getActivity());
            showToast("快捷方式创建成功");
        }
    }

    @Override
    public void cacheCleared(boolean result) {
        if(result){
            AppContext.getInstance().finishActivties();
            LoginActivity.launch(AppContext.getInstance());
        }
    }
}
